from selenium import webdriver
import time
from lxml import etree
import csv

html = "";
language = 0      # 语言
qb_course = 1     # 科目
qb_cartype = [1]  # 车型
qb_mark = 1       # 分值

web = webdriver.Chrome()


def getData(url):
    web.get('https://www.jiakaobaodian.com/mnks/exercise/0-moto-kemu3-beijing.html?id=986300')
    time.sleep(5)
    web.find_element_by_xpath('//button[@ref="xiangqing"]').click()
    web.find_element_by_xpath('//input[@class="checkbox-next"]').click()


def forNextTest():
    web.find_element_by_xpath('//div[@class="options-w left"]//p[@data-a-label="A"]').click()  # 答题
    leftHtml = web.find_element_by_xpath('//div[@class="left"]').get_attribute('innerHTML')
    getTest(leftHtml)


def getTest(htmlTxt):
    tree = etree.HTML(htmlTxt)
    res = tree.xpath('//p[@class="timu-text"]//text()')
    optionList = tree.xpath('//div[@class="options-w left"]//text()')
    qb_qtype = tree.xpath('//div[@class="option-type-msg"]//text()')[0].split("，")[0]

    # 题干
    title_num = res[0].split("/")[0]
    title = '、'.join(res[0].split('、')[1:])
    title = title_num + "、" + title

    #图片
    qb_pictxt = ""
    answer_wHtml = web.find_element_by_xpath('//div[@class="answer-w clearfix"]').get_attribute('innerHTML')
    if(answer_wHtml.find("media-w right")>-1):
        qb_pictxt = ",".join(str(i) for i in tree.xpath('//div[@class="media-w right"]/img/@src'))



    # 题目类型
    if (qb_qtype == "判断题"):
        qb_qtype = 2
    elif (qb_qtype == "单选题"):
        qb_qtype = 0
    else:
        qb_qtype = 1
        web.find_element_by_xpath('//button[@ref="btnAnswerOk"]').click()

    html = web.find_element_by_xpath('//div[@class="left"]').get_attribute('innerHTML')
    tree = etree.HTML(html)
    optionsucc = tree.xpath('//p[@class="success"]//text()')
    optionsuccbug = tree.xpath('//p[@class="success bug"]//text()')
    xiangjie = tree.xpath('//div[@class="xiangjie"]//div[@class="content"]//text()')
    # 选项
    jsonArray = [];
    for num in optionList:
        json = {"id": num.split('、')[0], "content": '、'.join(num.split('、')[1:])}
        jsonArray.append(json)

    # 答案
    anwser = [];
    anwserAll = []
    anwserStr = ""
    if (len(optionsucc) == 0):
        anwser = optionsuccbug
    else:
        anwser = optionsucc + optionsuccbug

    for num in anwser:
        anwserAll.append('、'.join(num.split('、')[1:]))
    anwserStr = ",".join(str(i) for i in anwserAll)

    # 详解
    xiangjiestr = "";
    for vue in xiangjie:
        xiangjiestr += vue;

    # 写入文件
    with open('B.csv', 'a+', encoding='utf-8-sig', newline='') as f:
        csv_writer = csv.writer(f)
        for i in range(0, len(qb_cartype)):
            csv_writer.writerow(
                [language, qb_course, qb_cartype[i], qb_qtype, title, qb_pictxt, jsonArray,
                 anwserStr, qb_mark,
                 xiangjiestr])

    web.find_element_by_xpath('//button[@ref="next"]').click()
    time.sleep(0.5)


getData("")
for i in range(0, 3000):
    forNextTest()
